# write by hashaki
# first edit on 2018/11/25
# last change on 2018/11/26
import sys
import os
sys.path.append(os.getcwd())

def downloadTheRate():
    '''下载历史成交价，单条数据'data':[{'tradeID': 108032354, 'date': '2018-11-25 02:03:17', 'timestamp': 1543125797, 'type': 'sell', 'rate': 3.1833, 'amount': 6, 'total': 19.0998},...]'''
    from hashakisea.api.digital.gateio import GateIO
    from pandas.core.frame import DataFrame
    a=GateIO()

    save_path='C:/hashaki/eosRate.csv'
    target_symbol='eos_usdt'
    num=108032354
    while 1:
        tid='/{}'.format(str(num))
        ask=target_symbol+tid
        print(ask)
        data=a.tradeHistory(ask)['data']

        dataDict={'date':[],'rate':[],'amount':[]}
        for i in data:
            dataDict['date'].append(i['date'])
            dataDict['rate'].append(float(i['rate']))
            dataDict['amount'].append(float(i['amount']))

        data_out=DataFrame(dataDict)
        data_out.to_csv(save_path,mode='a',header=False)
        num=int(data[-1]['tradeID'])+1

def loadTheRate():
    '''下载好价格数据之后，再加载数据，这是加载数据'''
    import pandas as pd
    load_path='C:/hashaki/eosRate.csv'
    data=pd.read_csv(load_path,usecols=[1,3])    # 抽取第几列
    # print(data['rate'][0])   # 8.275
    return data

def testSimilerForTensorflow():
    '''使用tensorflow线性回归测试成交价格与成交量之间的关系'''
    from hashakisky.modules.similerRate.similerRateByTensorflow import SimilerByTensorflow
    import numpy as np
    test=SimilerByTensorflow()
    print('开始 读取数据')
    data=loadTheRate()
    amount=data.iloc[:,0]
    rate=data.iloc[:,1]
    data1=np.array(amount)
    data2=np.array(rate)
    print('数据处理完毕')
    test.analyzerPriceItSelf_LinearRegretion(data1,data2)
    
if __name__=='__main__':
    testSimilerForTensorflow()